package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.dto.DishDto;
import com.example.entity.Category;
import com.example.entity.Dish;
import com.example.entity.DishFlavor;
import com.example.service.CategoryService;
import com.example.service.DishFlavorService;
import com.example.service.DishService;
import com.example.service.impl.CategoryServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 菜品管理(Dish)表控制层
 *
 * @author makejava
 * @since 2022-05-23 13:55:18
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    /**
     * 服务对象
     */
    @Resource
    private DishService dishService;
    @Resource
    private CategoryServiceImpl categoryService;
    @Resource
    private DishFlavorService dishFlavorService;
    @GetMapping("/page")
    public R<Page> pageOfDish(int page,int pageSize,String name){
        LambdaQueryWrapper<Dish>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),Dish::getName,name);
        Page<Dish> pageinfo =new Page(page,pageSize);
        Page<DishDto>pageInfo=new Page<>();
        dishService.page(pageinfo,queryWrapper);
        BeanUtils.copyProperties(pageinfo,pageInfo,"records");
        List<Dish>records=pageinfo.getRecords();
        List<DishDto>list=records.stream().map((item)->{
            DishDto dishDto=new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Category category=categoryService.getById(item.getCategoryId());
            dishDto.setCategoryName(category.getName());
            return dishDto;
        }).collect(Collectors.toList());
        pageInfo.setRecords(list);
        return R.success(pageInfo);
    }
    @PostMapping
    public R<String> post(@RequestBody DishDto dishDto) {
        dishDto.setSort(1);
        dishDto.setIsDeleted(0);
        int a= dishService.saveWithFlavor(dishDto);
        if (a>0) {
            return R.success("加入成功");
        } else {
            return R.error("添加失败");

        }

    }
    @GetMapping("/{id}")
    public R<DishDto>selectDishofid(@PathVariable Long id){
        DishDto dishDto=dishService.getDishDtoById(id);
        return R.success(dishDto);
    }
   @PutMapping
    public R<String>putNewDish(@RequestBody DishDto dishDto){
        int a=dishService.updateNewDish(dishDto);
        if(a>0){
            return R.success("修改成功");
        }else{
            return R.error("修改失败未知错误");
        }

   }
   @DeleteMapping
    public R<String>deleteDish(String ids){
        String[]arr=ids.split(",");
       for (int i = 0; i <arr.length ; i++) {
           Long id=Long.parseLong(arr[i]);
           dishService.updateIsdelete(id);
       }

        return R.success("删除成功");

   }
   @PostMapping("/status/{status}")
    public R<String>updatestatus(@PathVariable int status,String ids){
        log.info("状态{},id{}",status,ids);
        String arr[]=ids.split(",");
       for (int i = 0; i <arr.length; i++) {
           dishService.updatethisstatus(Long.parseLong(arr[i]));
       }

        return R.success("修改成功");
   }
//   @GetMapping("/list")
//    public R<List<Dish>>getDish(Long categoryId){
//        LambdaQueryWrapper<Dish>queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(Dish::getCategoryId,categoryId);
//        queryWrapper.eq(Dish::getStatus,1);
//        List<Dish>dishes=dishService.list(queryWrapper);
//        return R.success(dishes);
//   }
@GetMapping("/list")
public R<List<DishDto>>getDish(Long categoryId){
    LambdaQueryWrapper<Dish>queryWrapper=new LambdaQueryWrapper<>();
    queryWrapper.eq(Dish::getCategoryId,categoryId);
    queryWrapper.eq(Dish::getStatus,1);
    List<Dish>dishes=dishService.list(queryWrapper);
    List<DishDto>list=dishes.stream().map((item)->{
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(item,dishDto);
        Long dishid=item.getId();
        LambdaQueryWrapper<DishFlavor>queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(DishFlavor::getDishId,dishid);
        dishDto.setFlavors(dishFlavorService.list(queryWrapper1));
        Category category=categoryService.getById(item.getCategoryId());
        dishDto.setCategoryName(category.getName());
        return dishDto;
    }).collect(Collectors.toList());
    return R.success(list);
}


}